/*
Copyright 2009 Johan Maasing, Mikael Forsberg

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package com.google.code.pj2r;

import com.google.code.pj2r.resources.MarshallerFactory;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.pj2r.resources.ResourceFactoryImpl;
import com.google.code.pj2r.resources.ResourceDispatcherDelegate;

public class ResourceDispatcher extends HttpServlet {

	private static final String RESOURCE_PACKAGE = "resourcePackage";
	private static final String MARSHALLER_CLASSNAME = "marshaller";
	private ResourceDispatcherDelegate delegate;

	public void init() throws ServletException {
		final String packageName = getServletConfig().getInitParameter(RESOURCE_PACKAGE);
		if (packageName == null) {
			final String message = "Missing configuration, web.xml should contain a servlet init parameter named: " + RESOURCE_PACKAGE;
			Logger.getLogger(ResourceDispatcher.class.getName()).log(Level.SEVERE, null, message);
			throw new ServletException(message);
		}
		ResourceFactory resourceFactory = new ResourceFactoryImpl(packageName);

		final String marshallerParam = getServletConfig().getInitParameter(MARSHALLER_CLASSNAME);
		try {
			Marshaller marshaller = MarshallerFactory.getMarshaller(marshallerParam);
			delegate = new ResourceDispatcherDelegate(resourceFactory, marshaller);
		} catch (InstantiationException ex) {
			Logger.getLogger(ResourceDispatcher.class.getName()).log(Level.SEVERE, null, ex);
			throw new ServletException(ex);
		} catch (IllegalAccessException ex) {
			Logger.getLogger(ResourceDispatcher.class.getName()).log(Level.SEVERE, null, ex);
			throw new ServletException(ex);
		} catch (ClassNotFoundException ex) {
			Logger.getLogger(ResourceDispatcher.class.getName()).log(Level.SEVERE, null, ex);
			throw new ServletException(ex);
		}

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		delegate.doGet(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		delegate.doPost(request, response);
	}

	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		delegate.doDelete(request, response);
	}

	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		delegate.doPut(request, response);
	}
}
